/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xinxi.dms.view;

import com.xinxi.dms.po.Room;
import com.xinxi.dms.service.IRoomService;
import com.xinxi.dms.service.RoomServiceIMPL;
import com.xinxi.dms.util.FrameUtil2;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author zhigu
 */
public class RoomFrame extends javax.swing.JInternalFrame {
    IRoomService roomService = new RoomServiceIMPL();
    /**
     * Creates new form RoomFrame
     */
    public RoomFrame() {
        initComponents();
        List<Room> list = roomService.findAll();
        refresh(list);
        //禁用修改和删除
        this.btnupdate.setEnabled(false);
        this.btndelete.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabRoom = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtroomid = new javax.swing.JTextField();
        txtmoney = new javax.swing.JTextField();
        txtbuildingno = new javax.swing.JTextField();
        txtunitno = new javax.swing.JTextField();
        txtroomno = new javax.swing.JTextField();
        txtremarks = new javax.swing.JTextField();
        btninto = new javax.swing.JButton();
        btncancel = new javax.swing.JButton();
        btndelete = new javax.swing.JButton();
        btnupdate = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("房间信息管理");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        tabRoom.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "编号", "楼号", "单元号", "房间号", "金额", "容纳人数", "已入住人数", "状态"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                true, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabRoom.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                tabRoomAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        tabRoom.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabRoomMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabRoom);
        if (tabRoom.getColumnModel().getColumnCount() > 0) {
            tabRoom.getColumnModel().getColumn(0).setResizable(false);
            tabRoom.getColumnModel().getColumn(1).setResizable(false);
            tabRoom.getColumnModel().getColumn(2).setResizable(false);
            tabRoom.getColumnModel().getColumn(3).setResizable(false);
            tabRoom.getColumnModel().getColumn(4).setResizable(false);
            tabRoom.getColumnModel().getColumn(5).setResizable(false);
            tabRoom.getColumnModel().getColumn(6).setResizable(false);
            tabRoom.getColumnModel().getColumn(7).setResizable(false);
        }

        jLabel1.setText("编号：");

        jLabel2.setText("金额：");

        jLabel3.setText("楼号：");

        jLabel4.setText("容纳人数：");

        jLabel5.setText("单元号：");

        jLabel6.setText("房间号：");

        txtroomid.setEditable(false);

        btninto.setText("添加");
        btninto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnintoActionPerformed(evt);
            }
        });

        btncancel.setText("取消");
        btncancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncancelActionPerformed(evt);
            }
        });

        btndelete.setText("删除");
        btndelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btndeleteActionPerformed(evt);
            }
        });

        btnupdate.setText("修改");
        btnupdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnupdateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btninto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnupdate)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btndelete)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btncancel))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtmoney, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(10, 10, 10)
                                .addComponent(txtroomid)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtbuildingno, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                            .addComponent(txtremarks))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtunitno, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtroomno, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(61, 61, 61)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(txtroomid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtbuildingno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtunitno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtroomno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(82, 82, 82)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(txtmoney, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtremarks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btninto)
                    .addComponent(btncancel)
                    .addComponent(btndelete)
                    .addComponent(btnupdate))
                .addGap(34, 34, 34))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
        FrameUtil2.framemap.remove(RoomFrame.class.getName());
    }//GEN-LAST:event_formInternalFrameClosing

    private void btnintoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnintoActionPerformed
        // TODO add your handling code here:
        String buildingno = this.txtbuildingno.getText().trim();
        String unitno = this.txtunitno.getText().trim();
        String roomno = this.txtroomno.getText().trim();
        String smoney = this.txtmoney.getText().trim();
        double money = Double.parseDouble(smoney);
        String remarks = this.txtremarks.getText().trim();
        
        Room room = new Room(buildingno,unitno,roomno,money,remarks,0,"未开放");
        Room r= roomService.getByRoom(room);
        if(r!=null){
            JOptionPane.showMessageDialog(this, "房间已存在，请重新输入");
        }else{
            boolean result = roomService.add(room);
            if(result){
                JOptionPane.showMessageDialog(this, "添加成功");
                List<Room> list = roomService.findAll();
                refresh(list);
                cancel();
            }else{
                JOptionPane.showMessageDialog(this, "添加失败");
            }
        }
        //查询房间是否存在
        
    }//GEN-LAST:event_btnintoActionPerformed

    private void btncancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelActionPerformed
        // TODO add your handling code here:
        cancel();
        this.btninto.setEnabled(true);
    }//GEN-LAST:event_btncancelActionPerformed

    private void tabRoomAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_tabRoomAncestorAdded
        // TODO add your handling code here:
    }//GEN-LAST:event_tabRoomAncestorAdded

    private void tabRoomMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabRoomMouseClicked
        // TODO add your handling code here:
        //先获取到你所选中的行
        int row =this.tabRoom.getSelectedRow();
        //将选中行中的内容分别输入到下面的输入框中
        this.txtroomid.setText(this.tabRoom.getValueAt(row, 0).toString());
        this.txtbuildingno.setText(this.tabRoom.getValueAt(row, 1).toString());
        this.txtunitno.setText(this.tabRoom.getValueAt(row, 2).toString());
        this.txtroomno.setText(this.tabRoom.getValueAt(row, 3).toString());
        this.txtmoney.setText(this.tabRoom.getValueAt(row, 4).toString());
        this.txtremarks.setText(this.tabRoom.getValueAt(row, 5).toString());
        //禁用按钮
        this.btninto.setEnabled(false);
        
        this.txtbuildingno.setEditable(false);
        this.txtunitno.setEditable(false);
        this.txtroomno.setEditable(false);
        
        this.btnupdate.setEnabled(true);
        this.btndelete.setEnabled(true);
    }//GEN-LAST:event_tabRoomMouseClicked

    private void btnupdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnupdateActionPerformed
        // TODO add your handling code here:
        String sid = this.txtroomid.getText();
        Integer roomid = Integer.parseInt(sid);
        String smoney = this.txtmoney.getText();
        double money = Double.parseDouble(smoney);
        String remarks = this.txtremarks.getText();
        
        Room room =new Room(roomid,money,remarks);
        
        boolean result = roomService.update(room);
        if(result){
            JOptionPane.showMessageDialog(this, "修改成功");
            cancel();//清空输入框
            
            List<Room> list = roomService.findAll();
            refresh(list);
        }else
            JOptionPane.showMessageDialog(this, "修改失败");
    }//GEN-LAST:event_btnupdateActionPerformed

    private void btndeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btndeleteActionPerformed
        // TODO add your handling code here:
        Integer roomid = Integer.parseInt(this.txtroomid.getText());
        boolean result = roomService.delete(roomid);
        if(result){
            JOptionPane.showMessageDialog(this, "删除成功");
            cancel();//清空输入框
            
            List<Room> list = roomService.findAll();
            refresh(list);
        }else
            JOptionPane.showMessageDialog(this, "删除失败");
    }//GEN-LAST:event_btndeleteActionPerformed
    private void cancel(){
        this.txtroomid.setText("");
        this.txtbuildingno.setText("");
        this.txtunitno.setText("");
        this.txtroomno.setText("");
        this.txtmoney.setText("");
        this.txtremarks.setText("");
        
        this.txtbuildingno.setEditable(true);
        this.txtunitno.setEditable(true);
        this.txtroomno.setEditable(true);
        this.btnupdate.setEnabled(false);
        this.btndelete.setEnabled(false);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btncancel;
    private javax.swing.JButton btndelete;
    private javax.swing.JButton btninto;
    private javax.swing.JButton btnupdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabRoom;
    private javax.swing.JTextField txtbuildingno;
    private javax.swing.JTextField txtmoney;
    private javax.swing.JTextField txtremarks;
    private javax.swing.JTextField txtroomid;
    private javax.swing.JTextField txtroomno;
    private javax.swing.JTextField txtunitno;
    // End of variables declaration//GEN-END:variables

    private void refresh(List<Room> list) {
        DefaultTableModel dtm = (DefaultTableModel) this.tabRoom.getModel();
        while(dtm.getRowCount()>0){
            //从第0行开始删除
            dtm.removeRow(0);
        }//遍历集合
        for(Room r : list){
            Vector v =new Vector();
            v.add(r.getRoomid());
            v.add(r.getBuildingno());
            v.add(r.getUnitno());
            v.add(r.getRoomno());
            v.add(r.getMoney());
            v.add(r.getRemarks());
            v.add(r.getChecknumber());
            v.add(r.getState());
            dtm.addRow(v);
        }
    }
}
